# 数据库定义
# class (Base):
#     __tablename__ = ''
#
#     id: Mapped[int_pk]
#     name: Mapped[int] = mapped_column(ForeignKey('personbaseinfo.id'))

# 模板导出定义
import os,shutil
from  PySide6.QtWidgets import QFileDialog,QMessageBox
from sqlalchemy.orm import Session
from settings.publicvar import engine
from views.person.model.person import PersonBaseInfo

def downloadmodleFun(self):
    model_file_path = "state/model/人员扩展信息模板.xlsx"
    model_file_name = os.path.basename(model_file_path)
    save_path, _ = QFileDialog.getSaveFileName(self, '下载模板', model_file_name,
                                               f"(*.{model_file_name.split('.')[-1]})")
    shutil.copy2(model_file_path, save_path)
    QMessageBox.information(None, '下载成功', '恭喜你下载成功！')

import os,pandas as pd
# 导入数据处理
def importdataFun(self):
    url,a = QFileDialog.getOpenFileUrl(self,'导入数据')
    try:
        data = pd.read_excel(url.toLocalFile(), sheet_name=0, header=1,
                                 names=[],
                                 dtype={'IDcard': str, 'iphone': str}).drop('idx', axis=1)
        data = data.where(data.notna(), '')
        # 数据处理
        self.loadTableData()
    except:
        QMessageBox.critical(None,'导入失败','你没有选择文件！')

 # 搜索框数据处理-只获取用户数据
    def serearchFun(self):
        sereachdata = self.sereachedit.text().strip()
        if sereachdata == '':
            QMessageBox.critical(None, '错误', '搜索框不能为空！')
        else:
            with Session(engine) as session, session.begin():
                sereachName = session.query(PersonBaseInfo).filter(
                    PersonBaseInfo.name.like('%' + sereachdata + '%')).all()
                sereachIDcard = session.query(PersonBaseInfo).filter(
                    PersonBaseInfo.IDcard.like('%' + sereachdata + '%')).all()
                sereachiphone = session.query(PersonBaseInfo).filter(
                    PersonBaseInfo.iphone.like('%' + sereachdata + '%')).all()
                sereachpersonbase_type = session.query(PersonBaseInfo).filter(
                    PersonBaseInfo.person_type.like('%' + sereachdata + '%')).all()
                sereachData = sereachName + sereachIDcard + sereachiphone + sereachpersonbase_type
                others = []
                if sereachData != []:
                    for personbase in sereachData:
                        for other in personbase.:
                            others.append(other)
                    # 数据处理和加载QTable表
                else:
                    QMessageBox.critical(None,'没有数据','你搜索数据不存在！')